package com.ctsi.ssdc.service.impl;


import org.springframework.stereotype.Service;
import org.apache.commons.collections.CollectionUtils;
import com.ctsi.ssdc.domain.LitemallFeedback;
import com.ctsi.ssdc.domain.LitemallFeedbackExample;
import com.ctsi.ssdc.service.LitemallFeedbackService;
import com.ctsi.ssdc.repository.LitemallFeedbackRepository;
import org.apache.commons.lang3.StringUtils;

import java.time.LocalDateTime;
import java.time.ZonedDateTime;
import java.util.List;
import java.util.Objects;
import com.ctsi.ssdc.service.impl.StrengthenBaseServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import com.ctsi.ssdc.model.PageResult;
import org.springframework.data.domain.Pageable;
import com.github.pagehelper.PageHelper;
/**
 * Service Implementation for managing LitemallFeedback.
 *
 * @author ctsi-biyi-generator
 *
 */
@Service
public class LitemallFeedbackServiceImpl 
	extends StrengthenBaseServiceImpl<LitemallFeedbackRepository, LitemallFeedback, Integer, LitemallFeedbackExample> 
	implements LitemallFeedbackService {

    @Autowired
    LitemallFeedbackRepository litemallFeedbackRepository;


    /**
    * GET  /litemallFeedbacks : get the litemallFeedbacks firstStringBaseColumn.
    */
    @Override
    public PageResult<LitemallFeedback> findFirstStringColumn(String username,Pageable pageable){
        String str = username;
        if (Objects.nonNull(pageable)) {
            PageHelper.startPage(pageable.getPageNumber() + 1, pageable.getPageSize());
        }
        LitemallFeedbackExample litemallFeedbackExample = new LitemallFeedbackExample();
        String orderBy = getPageOrderBy(pageable);
        if(StringUtils.isNotEmpty(orderBy)) {
            litemallFeedbackExample.setOrderByClause(orderBy);
        }
        if (StringUtils.isEmpty(str)){
            litemallFeedbackExample.createCriteria();
        } else{
            litemallFeedbackExample.createCriteria().andUsernameLike("%" + str +"%");
        }
        List<LitemallFeedback>  data = litemallFeedbackRepository.selectByExample(litemallFeedbackExample);

        long count = 0L;
        if (CollectionUtils.isNotEmpty(data)){
            count = litemallFeedbackRepository.countByExample(litemallFeedbackExample);
        }
        return new PageResult<LitemallFeedback>(data,count,count);
    }

    @Override
    public Integer add(LitemallFeedback feedback) {
        feedback.setAddTime(ZonedDateTime.now());
        feedback.setUpdateTime(ZonedDateTime.now());
        return litemallFeedbackRepository.insertSelective(feedback);
    }

    private String getPageOrderBy(Pageable page) {
        if(page!= null && page.getSort() != null) {
            StringBuilder sb = new StringBuilder();
            page.getSort().forEach(sort -> sb.append(sort.getProperty())
            .append(" ").append(sort.getDirection()).append(","));
            if(sb.length() > 1) {
                return (sb.substring(0,sb.length()-1));
             }
        }
        return null;
    }
}
